package com.mrxu.monitor.notice.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mrxu.monitor.config.MonitorProperties;
import com.mrxu.monitor.notice.NotifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class QiYeWeiWinNotifyService implements NotifyService {

    @Autowired
    private MonitorProperties properties;

    @Override
    public void sendMsg(String content) {
        if(StrUtil.isBlank(properties.getNotify().qiYeWeiWinWebhook)) {
            return;
        }
        log.info("发送企业微信消息通知:{}",content);
        JSONObject message = new JSONObject();
        message.put("msgtype","text");
        JSONObject text = new JSONObject();
        text.put("content",content);
        JSONArray mentionedList = new JSONArray();
        mentionedList.add("@all");
        text.put("mentioned_list",mentionedList);
        message.put("text",text);
        String result = HttpUtil.post(properties.getNotify().qiYeWeiWinWebhook,message.toJSONString());
        log.info("发送企业微信消息通知返回:{}",result);
    }

}
